package com.swh.controller.Institutional;

import com.github.pagehelper.PageInfo;
import com.swh.entity.QualificationCondition;
import com.swh.entity.SysArea;
import com.swh.service.SysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/12/14 14:41
 * @Version: 1.0
 */
@Controller
@RequestMapping("/guguanjia/area")
public class SysAreaController {

    @Autowired
    private SysAreaService sysAreaService;

    @RequestMapping("/index")
    public String login() {
        return "/area/area";
    }

    @ResponseBody
    @RequestMapping("/findAll/{pageNum}/{pageSize}")
    public PageInfo<Map> findAll(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize,
                                 QualificationCondition condition) {
        return sysAreaService.selectPage(pageNum, pageSize, condition);
    }

    @ResponseBody
    @RequestMapping("/findAll")
    public List<SysArea> findAll() {
        SysArea sysArea = new SysArea();
        sysArea.setDelFlag("0");
        return sysAreaService.findAll(sysArea);
    }

    @RequestMapping("toUpdate")
    public String toUpdate() {
        return "area/save";
    }

    @RequestMapping("toSelect")
    public String toSelect() {
        return "area/select";
    }


    //根据id查询并且回写数据
    @ResponseBody
    @RequestMapping("/get")
    public Object get(Integer id) {
        SysArea byId = sysAreaService.findById(id);
        System.out.println(byId);
        return byId;
    }

    @RequestMapping("toModules")
    public String toModules() {
        return "modules/font-awesome";
    }


    @RequestMapping(value = "/toDemand", method = RequestMethod.GET)
    public String toDemand() {
        return "/area/detail";
    }


    //修改
    @ResponseBody
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public void updateById(@RequestBody SysArea sysArea) {
        sysArea.setUpdateDate(new Date());
        sysAreaService.update(sysArea);
    }


    //删除
    @ResponseBody
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.GET)
    public void deleteById(@PathVariable("id") Integer id) {
        sysAreaService.deleteChild(id);
    }

    //文件上传
    @ResponseBody
    @RequestMapping(value = "/upload", method = RequestMethod.GET)
    public String upload(MultipartFile file) throws IOException {
        sysAreaService.upload(file.getInputStream());
        return "ok";
    }



    //批量下载
    @ResponseBody
    @RequestMapping(value = "/Write")
    public void Write(HttpServletResponse response) throws IOException {
        response.setHeader("content-disposition", "attachment;filename="
                + new String("区域管理.xlsx".getBytes(), "iso8859-1"));
        sysAreaService.download(response.getOutputStream());
    }

//    @RequestMapping("/fileUpload2")
//    public String fileUpload2(HttpServletRequest req, MultipartFile upload) throws Exception {
//        System.out.println("SpringMvc完成文件上传");
//        //使用fileUpload组件完成文件上传
//        //上传的位置
//        String realPath = req.getSession().getServletContext().getRealPath("/uploads");
//        System.out.println(realPath);
//        //判断，该路径是否存在
//        File file=new File(realPath);
//        if (!file.exists()){
//            //创建该文件夹
//            file.mkdirs();
//        }
//        //说明是上传文件项，获取上传文件的名称
//        String filename = upload.getOriginalFilename();
//        //把文件的名称设置唯一值，uuid
//        String uuid = UUID.randomUUID().toString().replace("-", "");
//        filename=uuid+filename;
//        //完成文件上传
//        upload.transferTo(new File(realPath,filename));
//        return "success";
//    }


}
